<?php
/*
 * @Author: your name
 * @Date: 2019-11-19 14:04:28
 * @LastEditTime: 2021-01-06 13:17:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /ahsmp_3/application/common/model/HostRule.php
 */

namespace app\common\model;

use app\adminapiv3\model\learn_data\HostLearnDataModel;
use app\adminapiv3\service\module_manage\netfilter\FirewallRuleService;
use app\common\database\AutoIncreMentService;
use app\common\definition\HostDefinition;
use app\common\definition\netfilter\FirewallRuleDefinition;
use app\common\exception\CustomException;
use app\common\netfilter\Rule;
use think\Db;
use think\Model;

class HostRuleModel extends Model
{
    public $STATUS_ON = 1;//开启
    public $STATUS_OFF = 0;//关闭
    public $TYPE_HANDLE = 1;//用户自定义添加
    public $DIRECTION_OUT = 1;//出站流量
    /**
     * @name: 添加规则来自于 学习数据
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-04 18:34:05
     */
    public function insertFromLearnData ($host_id, $learn_id, $process, $have_mac, $os)
    {
        $date = date('Ymd');
        //子库中该主机的表名称
        $learnModel = new HostLearnDataModel();
        $learnModel->setHostId($host_id);
        $learnTableName = $learnModel->getFullTableName($host_id);
        $sql = 'SELECT
            `host_id`, CONCAT(' . $date . ', `id`) as `alias`, `direction`, `protocols`, ' .  ' `id` as `learn_id`, ' . 'INET_ATON(`src_ip`) as `src_ip_s`, INET_ATON(`src_ip`) as `src_ip_e`, INET_ATON(`dest_ip`) as `dest_ip_s`, INET_ATON(`dest_ip`) as `dest_ip_e`, `dest_port` as `dest_port_s`, `dest_port` as `dest_port_e`, if(`direction`=1, `dest_mac`, `src_mac`) as `mac_addr`
             FROM ' .$learnTableName. ' where id IN (' . $learn_id . ')';

        //执行sql
        $data = $learnModel->query($sql);
        if(!$data){
            throw new CustomException('学习数据不存在',201);
        }
        $init_fid = AutoIncreMentService::getTableAutoId('host_rule');
        if ($os == HostDefinition::LINUX_OPERATION_SYSTEM)
        {
            $scope = FirewallRuleDefinition::DEFAULT_NORMAL_RULE_REGION;
        }
        else
        {
            $scope = 'public,private,domain';
        }
        //补充字段
        foreach ($data as $key => $item)
        {
            $data[$key] = array_merge($data[$key],[
                'id' => $init_fid,
                'process' => $process,
                'status' => 1,
                'type' => 2,
                'have_mac_addr' => $have_mac,
                'src_port_s' => 0,
                'src_port_e' => 65535,
                'scope' => $scope,
                'fid' => $init_fid
            ]);
            $init_fid ++;
        }
        $firewall_a = array_map(function($v){
            $v['src_ip'] = $v['src_ip_s'] . '-' . $v['src_ip_e'];
            $v['dest_ip'] = $v['dest_ip_s'] . '-' . $v['dest_ip_e'];
            $v['src_port'] = $v['src_port_s'] . ':' . $v['src_port_e'];
            $v['dest_port'] = $v['dest_port_s'] . ':' . $v['dest_port_e'];
            return $v;
        }, $data);
        $FirewallRuleService = new FirewallRuleService();
        $firewall_a = $FirewallRuleService->processOut($firewall_a);
        $FirewallRuleService->addRealFirewallRule($firewall_a);
        $sql =  Db::name('host_rule')->fetchSql()->insertAll($data);
        //添加IGNORE关键字
        $pos = strpos($sql,'INTO');
        $str = substr($sql,0,$pos) . 'IGNORE' . ' INTO';
        $sql = str_replace('INSERT INTO',$str,$sql);
        return Db::execute($sql);
    }
}